"use strict";

const BaseController = require("./base");
const tokenKey = "qsweb-baiduAi-token-redis";
class BaiDuApiController extends BaseController {
  /**
   * @description 获取通过百度飞桨时需要的accss_token
   * @method GET
   * @author jacinto
   */
  async stsToken() {
    try {
      const redisRes =  await this.ctx.helper.getRedis(tokenKey);
      console.log('这是百度飞桨的redis缓存')
      console.log(redisRes)
      if (redisRes) {
        const res = await this.service.baiduapi.getStsToken();
        if (!res.success) throw res.msg;
        await this.ctx.helper.setRedis(
          tokenKey,
          res.data,
          //这个过期时间暂时和oss一样
          this.config.oss.tokenExpireTime
        ); // 获取token成功，缓存到redis
        await this.success(res.data, "success");
      } else {
        await this.success(redisRes, "success");
      }
    } catch (err) {
      await this.error(err);
    }
  }

}

module.exports = BaiDuApiController;
